import {defineStore} from 'pinia'
import {EditMenuItem, MenuItem} from "@/api/menu/type.ts";
import {getMenuList} from "@/api/menu";
import {buildMenuTree} from "@/utils/util.ts";


export const useMenuStore = defineStore('menu', () => {
    let menuItems = reactive<MenuItem[]>([])
    let addMenu_drawer = ref(false)
    let addChildMenu_drawer = ref(false)
    const editMenu_drawer = ref(false)
    const editType = ref(0)
    const addChildType = ref(0)
    const parentMenu = reactive<EditMenuItem>({
        ID: null,
        name: "",
        level: "",
        sort: null,
        url: "",
        request_type: "",
        parent_id: "",
        parent_name: "",
        icon: "",
        route_name: "",
        component: "",
        permissions: "",
        type: "",
    })
    // 获取菜单列表
    const getMenuListStore = async () => {
        const res = await getMenuList()
        if (res.code === 200) {
            menuItems.length = 0
            menuItems.push(...buildMenuTree(res.data))
        } else {
            ElMessage.error(res.msg)
        }
    }
    return {
        addMenu_drawer,
        menuItems,
        getMenuListStore,
        addChildMenu_drawer,
        parentMenu,
        editMenu_drawer,
        editType,
        addChildType
    }
})